# get_knight_hp_BGR.py
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np

figure = "hp_0124_4.png"
image_path = os.path.join("knight_hp_logs",figure)
hp_bar_img = cv2.imread(image_path)

KNIGHT_REF_IMG = cv2.imread("../knight_ref_hp.png")
# 识别小骑士血量图为整数数值信息
def detect_knight_hp_change(cmp_img,threshold=40):
    match = (cmp_img == KNIGHT_REF_IMG).all(axis=2)[0]  # shape: (570,)
    diff = np.diff(match.astype(np.int8), prepend=0, append=0)
    starts = np.where(diff == 1)[0]  # True 段开始
    ends = np.where(diff == -1)[0]  # True 段结束
    change = np.sum(ends - starts >= threshold)
    return change
print(detect_knight_hp_change(hp_bar_img))
'''
all_R = hp_bar_img[0,:,2]
all_G = hp_bar_img[0,:,1]
all_B = hp_bar_img[0,:,0]

# 绘制 R, G, B 三个通道的趋势图
plt.figure(figsize=(12, 6))
plt.plot(all_R, label='Red Channel (R)', color='red')
plt.plot(all_G, label='Green Channel (G)', color='green')
plt.plot(all_B, label='Blue Channel (B)', color='blue')

plt.title('RGB Values Across HP Bar (Horizontal)')
plt.xlabel('X Position (Pixel)')
plt.ylabel('Pixel Value (0-255)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
'''